package com.jxwifi.cloud.quickclean.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.basicactivity.BasicFrgment;
import com.bumptech.glide.Glide;
import com.jxwifi.cloud.quickclean.R;
import com.jxwifi.cloud.quickclean.app.AccountMgr;
import com.jxwifi.cloud.quickclean.app.PasswordLogin;
import com.jxwifi.cloud.quickclean.app.UrlPool;
import com.jxwifi.cloud.quickclean.bean.ProductInfoVoBean;
import com.jxwifi.cloud.quickclean.model.AuzImpl;
import com.jxwifi.cloud.quickclean.okhttp.DataJson_Cb;
import com.jxwifi.cloud.quickclean.okhttp.OkHttp;
import com.jxwifi.cloud.quickclean.okhttp.Params;
import com.jxwifi.cloud.quickclean.order.ProductDetailsActivity;
import com.jxwifi.cloud.quickclean.utils.ToastUtils;

import org.apache.http.util.TextUtils;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2019/6/4.
 */

public class ImagesListFragment extends BasicFrgment {

    @Bind(R.id.img_fg_list)
    ImageView mImgFgList;
    @Bind(R.id.img_no_opening)
    ImageView mImgNoOpening;
    private String imgUrl;
    private String productCode;
    private String ProductPriceCode = "";


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View root = inflater.inflate(R.layout.fg_img_listview, container, false);
        ButterKnife.bind(this, root);
        openEventBus();
        Log.w("oushuhua", "getProductPriceCode1 " + ProductPriceCode);
        imgUrl = getArguments().getString("detailurl".toLowerCase());
        productCode=getArguments().getString("product_code".toLowerCase());
        ProductPriceCode=getArguments().getString("product_price_code".toLowerCase());
        Log.w("oushuhua", "getProductPriceCode2 " + ProductPriceCode);
        if (!TextUtils.isEmpty(imgUrl)) {
            Glide.with(getActivity()).load(imgUrl).dontAnimate().into(mImgFgList);
        }
        if (TextUtils.isEmpty(ProductPriceCode) || "".equals(ProductPriceCode)) {
            mImgNoOpening.setVisibility(View.VISIBLE);
        } else {
            mImgNoOpening.setVisibility(View.GONE);
        }
        return root;
    }


    @OnClick({R.id.img_fg_list})
    void clickCK(View v) {
        switch (v.getId()) {
            case R.id.img_fg_list:
                if (!TextUtils.isEmpty(productCode) && !TextUtils.isEmpty(ProductPriceCode)) {
                    ProductDetails();
                } else {
                    Toast toast = Toast.makeText(mBaseContext, "该区域服务暂未开通，敬请期待", Toast.LENGTH_SHORT);
                    ToastUtils.showMyToast(toast, 3000);
                }
                break;
        }
    }

    public void initView(){
        if (!TextUtils.isEmpty(imgUrl)) {
            Glide.with(getActivity()).load(imgUrl).dontAnimate().into(mImgFgList);
        }
        if (TextUtils.isEmpty(ProductPriceCode) || "".equals(ProductPriceCode)) {
            mImgNoOpening.setVisibility(View.VISIBLE);
        } else {
            mImgNoOpening.setVisibility(View.GONE);
        }
    }

    public void ProductDetails() {
        Params params = new Params();
        params.add("productCode", productCode);
        params.add("productPriceCode", ProductPriceCode);
        OkHttp.post(UrlPool.POST_PRODUCT_DETAILS, params, new DataJson_Cb() {
            @Override
            public void onSuccess(int code, String data) {
                if (code == 0) {  //ProductInfoVoBean
                    ProductInfoVoBean mBean = JSON.parseObject(data, ProductInfoVoBean.class);
                    Intent intent = new Intent();
                    intent.setClass(mBaseContext, ProductDetailsActivity.class);
                    intent.putExtra("product_details", mBean);
                    startActivity(intent);
                } else {
                    PasswordLogin.passwordlogin(mBaseContext, TAG, AccountMgr.gettell(mBaseContext), AccountMgr.getpassword(mBaseContext));
                    if (PasswordLogin.mIsLogin) {
                        ProductDetails();
                    }
                }
            }

            @Override
            public void onFailure(int errorCode, String msg) {
                Toast toast = Toast.makeText(mBaseContext, msg, Toast.LENGTH_SHORT);
                ToastUtils.showMyToast(toast, 3000);
            }
        }, TAG);
    }
}
